package ru.ipo.dces.problemsviewer;

import info.clearthought.layout.TableLayout;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicCheckBoxUI;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 02.06.11
 * Time: 13:14
 */
public class ProblemHeaderComponent extends JPanel {

    private final JTabbedPane pane;
    private final JButton closeButton;
    private final HistoryButton historyButton;
    private static final TableLayout layout = new TableLayout(
            new double[]{TableLayout.FILL, 10, TableLayout.PREFERRED, TableLayout.PREFERRED},
            new double[]{TableLayout.FILL}
    );

    private static final int SIZE = 16;

    public ProblemHeaderComponent(JTabbedPane pane) {
        this.pane = pane;

        setLayout(layout);

        JLabel header = new JLabel() {
            @Override
            public String getText() {
                ProblemHeaderComponent thisComponent = ProblemHeaderComponent.this;
                int ind = getTabIndex();
                return ind == -1 ? null : thisComponent.pane.getTitleAt(ind);
            }
        };

        setOpaque(false);

        add(header, "0 0");

        closeButton = new CloseButton();
        historyButton = new HistoryButton();
        add(historyButton, "2 0");
        add(closeButton, "3 0");
    }

    public class CloseButton extends JButton {

        private CloseButton() {
            setPreferredSize(new Dimension(SIZE, SIZE));
            setBorder(new LineBorder(Color.blue));
            setFocusable(false);
            setUI(new BasicButtonUI());
            setContentAreaFilled(false);
            setRolloverEnabled(true);

            setBorderPainted(false);

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseExited(MouseEvent e) {
                    setBorderPainted(false);
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    setBorderPainted(true);
                }
            });
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.black);
            if (getModel().isPressed())
                g.translate(1, 1);
            int padding = 4;
            ((Graphics2D) g).setStroke(new BasicStroke(2));
            g.drawLine(padding, padding, SIZE - padding, SIZE - padding);
            g.drawLine(padding, SIZE - padding, SIZE - padding, padding);
        }

        public ProblemHeaderComponent getHeader() {
            return ProblemHeaderComponent.this;
        }

    }

    public class HistoryButton extends JCheckBox {

        public HistoryButton() {
            setText(null);

            setPreferredSize(new Dimension(SIZE, SIZE));
            setBorder(new LineBorder(Color.blue));
            setFocusable(false);
            setUI(new BasicCheckBoxUI());
            setContentAreaFilled(false);
        }

        public ProblemHeaderComponent getHeader() {
            return ProblemHeaderComponent.this;
        }
    }

    private int getTabIndex() {
        return pane.indexOfTabComponent(this);
    }

    public JButton getCloseButton() {
        return closeButton;
    }

    public HistoryButton getHistoryButton() {
        return historyButton;
    }
}
